package com.pucong.gitee.database.backup.business.service.util.cmdOs;

import com.jcraft.jsch.*;

public class ChannelSftpSingleton {

    private static ChannelSftpSingleton instance;
    private ChannelSftp channelSftp;

    private ChannelSftpSingleton() {
    }

    public static ChannelSftpSingleton getInstance() {
        if (instance == null) {
            instance = new ChannelSftpSingleton();
        }
        return instance;
    }

    /**
     * 断开SFTP Channel、Session连接
     *
     * @throws Exception
     */
    public void closeChannel() throws Exception {
        if (channelSftp != null) {
            channelSftp.disconnect();
        }
        System.out.println("disconnected SFTP successfully!");
    }

    /**
     * 获得SFTP Channel
     *
     * @return ChannelSftp Instance
     * @throws JSchException
     */
    private ChannelSftp getChannelSftp(Session session) throws JSchException {
        // 打开SFTP通道
        Channel channel = session.openChannel("sftp");
        // 建立SFTP通道的连接
        channel.connect();
        return (ChannelSftp) channel;
    }
}
